package com.wuliming.gulimall.ware.service.impl;

import cn.hutool.core.util.StrUtil;
import com.wuliming.common.utils.R;
import com.wuliming.gulimall.ware.entity.PurchaseDetailEntity;
import com.wuliming.gulimall.ware.entity.PurchaseEntity;
import com.wuliming.gulimall.ware.feign.ProductFeignService;
import com.wuliming.gulimall.ware.service.PurchaseDetailService;
import com.wuliming.to.es.WareSkuStock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuliming.common.utils.PageUtils;
import com.wuliming.common.utils.Query;

import com.wuliming.gulimall.ware.dao.WareSkuDao;
import com.wuliming.gulimall.ware.entity.WareSkuEntity;
import com.wuliming.gulimall.ware.service.WareSkuService;
import org.springframework.transaction.annotation.Transactional;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    @Autowired
    private WareSkuDao wareSkuDao;


    @Autowired
    private ProductFeignService productFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WareSkuEntity> wrapper = new QueryWrapper<>();
        String skuId = (String) params.get("skuId");
        if (StrUtil.isNotEmpty(skuId)) {
            wrapper.eq("sku_id", skuId);
        }
        String wareId = (String) params.get("wareId");
        if (StrUtil.isNotEmpty(wareId)) {
            wrapper.eq("ware_id", wareId);
        }
        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void addStock(Long wareId, Long skuId, Integer skuNum) {
        Long sku = count(new QueryWrapper<WareSkuEntity>().eq("ware_id", wareId).eq("sku_id", skuId));
        WareSkuEntity wareSkuEntity = new WareSkuEntity();
        wareSkuEntity.setWareId(wareId);
        wareSkuEntity.setStock(skuNum);
        try {
            Map<String, Object> skuInfo = (Map<String, Object>) productFeignService.info(skuId).get("skuInfo");
            wareSkuEntity.setSkuName((String) skuInfo.get("skuName"));
        } catch (Exception e) {

        }
        wareSkuEntity.setStockLocked(0);
        wareSkuEntity.setSkuId(skuId);
        if (sku > 0) {
            wareSkuDao.addStock(wareSkuEntity);
        } else {
            save(wareSkuEntity);
        }
    }

    @Override
    public List<WareSkuStock> hasStock(List<Long> skuIds) {
        List<WareSkuStock> collect = skuIds.stream().map(skuId -> {
            WareSkuStock wareSkuStock = new WareSkuStock();
            Long count = baseMapper.getSkuStock(skuId);
            wareSkuStock.setSkuId(skuId);
            wareSkuStock.setHasStock(count==null?false:count>0);
            return wareSkuStock;
        }).collect(Collectors.toList());
        return collect;
    }

}